<?php


class User_locker_model extends Base_model {

    /**
     * 用户锁有效时间(秒), 默认为: 600秒(10分钟)
     */
    const VALIDATE_TIME_SECOND = 600;

    /**
     * status状态常量：1 已锁定   0 已解锁
     */
    const STATUS_LOCKED = 1;
    const STATUS_UNLOCKED = 0;


	/**
	 * 表名
	 * 
	 * @param string 
	 */
    public $_table_name = "user_locker";


	/**
	 * 错误信息
	 * 
	 * @param string 
	 */
    public $_error_info = "操作锁定，请稍后重试！";


    /**
     * 状态
     * @var tinyint 
     */
    public $status;


    /**
     * 用户ID
     * @var int 
     */
    public $user_id;


    /**
     * 创建时间
     * @var int 
     */
    public $create_time;


    /**
     * 插入数据前验证
     * 
     * @return boolean 
     */
    protected function prepare_insert()
	{
        if(empty($this->user_id))
		{
			return false;
		}

        if(empty($this->status))
		{
            $this->status = self::STATUS_LOCKED;
        }

        if(empty($this->create_time))
		{
            $this->create_time = time();
        }

        return true;
    }


    /**
     * 搜索
     * 
     * @return boolean true | false
     */
    public function search()
	{
        return true;
    }


    /**
     * 根据用户id加载数据给对象.
     *
     * @param int $user_id 用户id
     * @return boolean
     */
    public function retrieve_by_user_id($user_id)
	{
        if(empty($user_id))
		{
            return false;
        }

        $sql = "SELECT * FROM {$this->_table_name} WHERE user_id = ? ORDER BY id DESC LIMIT 1";

        $result = $this->db->query($sql, array($user_id));

		$re = false; //初始化返回结果

        if($result->num_rows() > 0)
		{
            $this->result_load($result->row());

            if(!$this->is_empty())
			{
				$re = true;
            }
        }

		$result->free_result();

		return $re;
	}


    /**
     * 锁定用户操作
     * 
     * @param int $user_id 用户id
     * @return boolean 
     */
    public function lock_by_user_id($user_id)
    {
        if(empty($user_id))
		{
			return false;
		}

		$this->user_id = $user_id;

		return $this->insert(); //返回是否锁定成功
    }


    /**
     * 检查用户是否被锁定
     * 
     * @param int $user_id 用户id
     * @return boolean 
     */
    public function check_locked_by_user_id($user_id)
    {
        if(empty($user_id))
		{
			return false;
		}

		//无锁定记录, 返回未锁定状态
		if(!$this->retrieve_by_user_id($user_id))
		{
			return false;
		}

		//未锁定状态
		if($this->status == self::STATUS_UNLOCKED)
		{
			return false;
		}

		//如果有锁定时间超过了锁定有效时间, 返回未锁定状态
		if((time() - $this->create_time) > self::VALIDATE_TIME_SECOND)
		{
			return false;
		}

		$this->_error_info = "操作锁定，" . intval((self::VALIDATE_TIME_SECOND) / 60) . "分钟后请重试！";

		return true; //返回为锁定状态
    }


    /**
     * 解除用户锁定状态
     * 
     * @param int $user_id 用户id
     * @return boolean
     */
    public function unlock_by_user_id($user_id) 
    {
        if(empty($user_id))
		{
			return false;
		}

		//直接删除用户的锁定记录
		$sql = "DELETE FROM {$this->_table_name} WHERE user_id = ?";

		$this->db->query($sql, array($user_id));

		//自动删除过期的锁定记录
		$this->auto_delete_expired();

		return true;
    }


    /**
     * 自动删除过期的锁定记录(达到自动维护的目的)
     * 
   * @return boolean
     */
	private function auto_delete_expired()
	{
		if((mt_rand() % 100) < 5)
		{
			$sql = "DELETE FROM {$this->_table_name} WHERE create_time < " . (time() - self::VALIDATE_TIME_SECOND * 2);

	    	$this->db->query($sql);
		}

		return true;
	}

}